# Load libraries ----
bcc.packages <- c(
  'rio',
  'dplyr',
  'ggplot2',
  'patchwork',
  'modelsummary',
  'gt',
  'gtsummary'
)

lapply(bcc.packages, require, character.only = TRUE)

# Import data ----
ds <- import('data/bcc.csv')

# Clean data ----
ds <- ds %>%
  mutate(
    full_treatment = factor(
      full_treatment,
      levels = c(
        'Control',
        'Credit_Claim',
        'Announce',
        'Protect',
        'Commiserate'
      )
    ),
    spending_cat_three = case_when(
      spending_cat == "transportation" ~ "transportation",
      spending_cat == "healthcare" ~ "healthcare",
      spending_cat == "employment" ~ "employment",
      full_treatment == "Control" ~
        sample(
          c("transportation", "healthcare", "employment"),
          size = n(),
          replace = TRUE
        )
    ),
    feeling_change = post_feeling - pre_feeling,
    effective_change = post_effective - pre_effective,
    represent_change = post_represent - pre_represent,
    responsible_change = post_responsible - pre_responsible,
    partyr = case_when(
      party == "Not very strong Democrat" | party == "Strong Democrat" ~ "dem",
      party == "Not very strong Republican" | party == "Strong Republican" ~
        "rep",
      party == "Independent" & independent == "More similar to Democrats" ~
        "ind",
      party == "Independent" & independent == "Neither" ~ "ind",
      party == "Independent" & independent == "More similar to Republicans" ~
        "ind"
    ),
    partytwo = case_when(
      party == "Not very strong Democrat" | party == "Strong Democrat" ~ "dem",
      party == "Not very strong Republican" | party == "Strong Republican" ~
        "rep",
      party == "Independent" & independent == "More similar to Democrats" ~
        "dem",
      party == "Independent" & independent == "More similar to Republicans" ~
        "rep"
    ),
    party_sen = case_when(
      party_sen == "D" | party_sen == "I" ~ "dem",
      party_sen == "R" ~ "rep"
    ),
    party_match = case_when(
      partyr == "dem" & party_sen == "dem" ~ 1,
      partyr == "rep" & party_sen == "rep" ~ 1,
      partyr == "dem" & party_sen == "rep" ~ -1,
      partyr == "rep" & party_sen == "dem" ~ -1,
      partyr == "ind" ~ 0
    ),
    party_match_f = as.factor(party_match),
    income = case_when(
      income == "Under $10,000" ~ 1,
      income == "$10,000-$19,999" ~ 2,
      income == "$20,000-$29,999" ~ 3,
      income == "$30,000-$39,999" ~ 4,
      income == "$40,000-$49,999" ~ 5,
      income == "$50,000-$59,999" ~ 6,
      income == "$60,000-$74,999" ~ 7,
      income == "$75,000-$89,999" ~ 8,
      income == "$90,000-$119,999" ~ 9,
      income == "$120,000-$159,999" ~ 10,
      income == "$160,000-$200,000" ~ 11,
      income == "Over $200,000" ~ 12
    ),
    gender = case_when(gender == 1 ~ "man", gender == 2 ~ "woman"),
    age = as.numeric(age),
    education = case_when(
      education %in% 1:5 ~ "Less_than_Bachelors",
      education %in% 6:8 ~ "Bachelors_or_higher"
    ),
    ideo = case_when(
      ideology == "Very Conservative" ~ 1,
      ideology == "Conservative" ~ 2,
      ideology == "Slightly Conservative" ~ 3,
      ideology == "Moderate" ~ 4,
      ideology == "Slightly Liberal" ~ 5,
      ideology == "Liberal" ~ 6,
      ideology == "Very Liberal" ~ 7
    ),
    black = ifelse(grepl("Black or African American", race), 1, 0),
    hisplat = ifelse(grepl("Hispanic or Latino", race), 1, 0),
    asam = ifelse(grepl("Asian or Pacific Islander", race), 1, 0),
    natam = ifelse(grepl("American Indian or Alaska Native", race), 1, 0),
    white = ifelse(grepl("White", race), 1, 0),
    multiracial = ifelse(grepl("Multi-racial", race), 1, 0),
    rother = ifelse(grepl("Other", race), 1, 0),
    race = case_when(
      race == "White" ~ "White",
      race == "Asian or Pacific Islander" ~ "Asian American",
      race == "Black or African American" ~ "Black",
      race == "Hispanic or Latino" ~ "Latino",
      grepl("Multi-racial", race) ~ "Multi-racial",
      race == "American Indian or Alaska Native" ~ "Other",
      race == "Other" ~ "Other",
      grepl(",", race) ~ "Other"
    ),
    hispanic = case_when(
      hispanic %in% c(1, 12, 15) ~ 0,
      hispanic %in% c(2:11, 13, 14, 16) ~ 1
    ),
    parent = factor(parent, levels = c("No", "Yes")),
    region = case_when(
      region == 1 ~ "Northeast",
      region == 2 ~ "Midwest",
      region == 3 ~ "South",
      region == 4 ~ "West"
    )
  )

# Formatting function
pvalue_format <- function(x) {
  z <- cut(
    as.numeric(x),
    breaks = c(-Inf, 0.01, 0.05, 0.1, Inf),
    labels = c("***", "**", "*", "")
  )
  as.character(z)
}

# Settings ----
options(scipen = 999)
options("modelsummary_format_numeric_latex" = "plain")
